package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.common.user.UserCommon;
import com.zhentao.minio.service.FileStorageService;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.pojo.car.ChargeCarBrand;
import com.zhentao.model.pojo.car.ChargeCarInfo;
import com.zhentao.model.pojo.car.Vo.ChargeCarInfoVO;
import com.zhentao.model.pojo.car.dto.ChargeCarInfoDto;
import com.zhentao.model.pojo.car.enums.AutomobileDisplacement;
import com.zhentao.model.pojo.car.enums.CarBattery;
import com.zhentao.model.pojo.car.enums.CarParameter;
import com.zhentao.model.pojo.user.ChargeUser;
import com.zhentao.service.ChargeCarBrandService;
import com.zhentao.service.ChargeCarInfoService;
import com.zhentao.mapper.ChargeCarInfoMapper;
import com.zhentao.user.ChargeUserThreadUtils;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 22115
 * @description 针对表【charge_car_info】的数据库操作Service实现
 * @createDate 2024-12-18 08:53:09
 */
@Service
public class ChargeCarInfoServiceImpl extends ServiceImpl<ChargeCarInfoMapper, ChargeCarInfo>
        implements ChargeCarInfoService {
    @Resource
    ChargeCarBrandService chargeCarBrandService;


    @Override
    public ResponseResult CarList() {
        ChargeUser chargeUserThreadLocal = ChargeUserThreadUtils.getChargeUserThreadLocal();
        Long id = chargeUserThreadLocal.getId();

        List<ChargeCarInfo> list = this.list(Wrappers.<ChargeCarInfo>lambdaQuery().eq(ChargeCarInfo::getUserId, id));

        List<ChargeCarInfoVO> chargeCarInfoVo = new ArrayList<>();
        for (ChargeCarInfo chargeCarInfo : list) {
            ChargeCarInfoVO chargeCarInfoVO = new ChargeCarInfoVO();
            BeanUtils.copyProperties(chargeCarInfo, chargeCarInfoVO);
            ChargeCarBrand one = chargeCarBrandService.getOne(Wrappers.<ChargeCarBrand>lambdaQuery().eq(ChargeCarBrand::getId, chargeCarInfo.getCarBrandId()).eq(ChargeCarBrand::getStatus, 0));
            chargeCarInfoVO.setCarBrandName(one.getBrandName());
            chargeCarInfoVO.setFuelTypeName(CarParameter.getCarParameter(chargeCarInfo.getFuelType()));
            chargeCarInfoVO.setDisplacementName(AutomobileDisplacement.getAutomobileDisplacement(chargeCarInfo.getDisplacement()));
            chargeCarInfoVO.setElectromagneticTypeName(CarBattery.getCarBattery(chargeCarInfo.getElectromagneticType()));
//            chargeCarInfoVO.setBatteryCapacityName(BatteryCapacity.getBatteryCapacity(chargeCarInfo.getBatteryCapacity()));
            chargeCarInfoVo.add(chargeCarInfoVO);
        }


        return ResponseResult.okResult(chargeCarInfoVo);
    }

    @Resource
    FileStorageService fileStorageService;
    @Override
    @SneakyThrows
    public ResponseResult getCarsSaveMinio(MultipartFile multipartFile) {
        String path = fileStorageService.uploadImgFile(UserCommon.CHARGE_CAR_INFO_AVATAR_IMG, multipartFile.getOriginalFilename(), multipartFile.getInputStream());
        return ResponseResult.okResult(path);
    }

    @Override
    public ResponseResult getCarsSave(ChargeCarInfoDto chargeCarInfoDto) {

        if (!StringUtils.isNotBlank(chargeCarInfoDto.getCarNo())){
            return ResponseResult.errorResult(500,"参数不能为空");
        }
        ChargeCarInfo chargeCarInfo = new ChargeCarInfo();
        BeanUtils.copyProperties(chargeCarInfoDto,chargeCarInfo);
        String CarNo = chargeCarInfoDto.getProvince() + chargeCarInfoDto.getCarNo();
        List<ChargeCarInfo> list = this.list();
        for (ChargeCarInfo carInfo : list) {
            if (carInfo.getCarNo().equals(CarNo)){
                return ResponseResult.errorResult(500, "重复车牌号");
            }
        }
        ChargeUser chargeUserThreadLocal = ChargeUserThreadUtils.getChargeUserThreadLocal();
        chargeCarInfo.setUserId(chargeUserThreadLocal.getId());
        chargeCarInfo.setCarNo(CarNo);
        boolean save = this.save(chargeCarInfo);
        if (save){
            return ResponseResult.okResult("添加成功");
        }
        return ResponseResult.errorResult(500,"添加失败");

    }

    @Override
    public ResponseResult getCarsUpdate(ChargeCarInfoDto chargeCarInfoDto) {
        if (chargeCarInfoDto==null){
            return ResponseResult.errorResult(500,"参数不能为空");
        }
        String CarNo = chargeCarInfoDto.getProvince() + chargeCarInfoDto.getCarNo();

        ChargeCarInfo chargeCarInfo = new ChargeCarInfo();
        BeanUtils.copyProperties(chargeCarInfoDto,chargeCarInfo);
        chargeCarInfo.setCarNo(CarNo);

        boolean b = this.updateById(chargeCarInfo);
//        boolean update = this.update(Wrappers.<ChargeCarInfo>lambdaUpdate().set(ChargeCarInfo::getCarNo, CarNo).set(ChargeCarInfo::getCarBrandId, chargeCarInfo.getCarBrandId()).set(ChargeCarInfo::getFuelType, chargeCarInfo.getFuelType()).set(ChargeCarInfo::getDisplacement, chargeCarInfo.getDisplacement()).set(ChargeCarInfo::getAvatarImg, chargeCarInfo.getAvatarImg()).set(ChargeCarInfo::getElectromagneticType, chargeCarInfo.getElectromagneticType()).set(ChargeCarInfo::getBatteryCapacity, chargeCarInfo.getBatteryCapacity()).set(ChargeCarInfo::getRanges, chargeCarInfo.getRanges()).set(ChargeCarInfo::getVehicleIdentifyNo, chargeCarInfo.getVehicleIdentifyNo()).set(ChargeCarInfo::getMaxSpeed, chargeCarInfo.getMaxSpeed()).set(ChargeCarInfo::getAccelerationTime, chargeCarInfo.getAccelerationTime()).set(ChargeCarInfo::getModelParameters, chargeCarInfo.getModelParameters()).set(ChargeCarInfo::getEnergyConsumptionDesc, chargeCarInfo.getEnergyConsumptionDesc()).set(ChargeCarInfo::getOtherDesc, chargeCarInfo.getOtherDesc()).eq(ChargeCarInfo::getId, chargeCarInfo.getId()));
        if (b){
            return ResponseResult.okResult("修改成功");
        }
        return ResponseResult.errorResult(500,"修改失败");
    }

    @Override
    public ResponseResult getCarsBatteryCapacity(ChargeCarInfoDto chargeCarInfoDto) {
        if (!StringUtils.isNotBlank(chargeCarInfoDto.getVehicleIdentifyNo())){
            return ResponseResult.errorResult(500,"参数不能为空");
        }
        ChargeCarInfo one = this.getOne(Wrappers.<ChargeCarInfo>lambdaQuery().eq(ChargeCarInfo::getVehicleIdentifyNo, chargeCarInfoDto.getVehicleIdentifyNo()));
        Integer batteryCapacity = one.getBatteryCapacity();
        int i = batteryCapacity - chargeCarInfoDto.getBatteryCapacity();


        return ResponseResult.okResult(i);
    }
}




